#!/bin/bash
#
#  pkgutils
#
#  Copyright (c) 2000-2005 Per Liden
#  Copyright (c) 2006-2007 by CRUX team (http://crux.nu)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#  USA.
#

THIS_SCRIPT_PATH="$(readlink -f "$(type -P $0 || echo $0)")"
THIS_SCRIPT_DIR="$(dirname "$THIS_SCRIPT_PATH")"
source $THIS_SCRIPT_DIR/color_messages.sh
trap "interrupted" SIGHUP SIGINT SIGQUIT SIGTERM
color_mesg

get_filename() {
	local FILE="`echo $1 | sed 's|^.*://.*/||g'`"

	if [ "$FILE" != "$1" ]; then
		FILE="$PKGMK_SOURCE_DIR/$FILE"
	fi

	echo $FILE
}

get_basename() {
	local FILE="`echo $1 | sed 's|^.*://.*/||g'`"
	echo $FILE
}

check_pkgfile() {
	if [ "`type -t build`" != "function" ]; then
        abort "$(gettext "Function 'build' not specified in %s.")" "$PKGMK_PKGFILE"
	fi
}

check_directory() {
	if [ ! -d $1 ]; then
        abort "$(gettext "Directory '%s' does not exist.")" "$1"
	elif [ ! -x $1 ] || [ ! -r $1 ]; then
        abort "$(gettext "Directory '%s' is not readable.")" "$1"
	fi
}
unpack_source() {
	local FILE LOCAL_FILENAME COMMAND

	for FILE in ${source[@]}; do
		LOCAL_FILENAME=`get_filename $FILE`
		case $LOCAL_FILENAME in
			*.tar.gz|*.tar.Z|*.tgz|*.tar.bz2|*.tbz2|*.tar.xz|*.txz|*.tar.lzma|*.zip|*.rpm)
				COMMAND="tar -p -o -C $SRC -xf $LOCAL_FILENAME" ;;
			*)
				COMMAND="cp $LOCAL_FILENAME $SRC" ;;
		esac

		echo "$COMMAND"

		$COMMAND

		if [ $? != 0 ]; then
			if [ "$PKGMK_KEEP_WORK" = "no" ]; then
				rm -rf $PKGMK_WORK_DIR
			fi
            abort "$(gettext "Building '%s' failed.")" "$TARGET"
		fi
	done
}

build_package() {
	local BUILD_SUCCESSFUL="no"

	export PKG="/"
	export SRC="$PKGMK_WORK_DIR/src"
	umask 022

	cd $PKGMK_ROOT
	rm -rf $PKGMK_WORK_DIR
	mkdir -p $SRC


	if [ "$UID" == "0" ]; then
		abort "$(gettext "Packages should NOT be built as root.")"
	fi

	printf "$(gettext "=======> Building %s\n")" "$TARGET"

	unpack_source

	cd $SRC
	(set -e -x ; build)


	if [ $? = 0 ]; then
		BUILD_SUCCESSFUL="yes"

	fi
        if [ "$PKGMK_KEEP_WORK" = "no" ]; then
                rm -rf $PKGMK_WORK_DIR
        fi

	if [ "$BUILD_SUCCESSFUL" = "yes" ]; then
		printf "$(gettext "=======> Building %s succeeded.\n")" "$TARGET"
	else
		abort "$(gettext "Building '%s' failed.")" "$TARGET"
	fi
}


clean() {
	local FILE LOCAL_FILENAME

	if [ -f $TARGET ]; then
        msg "$(gettext "Removing %s")" "$TARGET"
		rm -f $TARGET
	fi

	for FILE in ${source[@]}; do
		LOCAL_FILENAME=`get_filename $FILE`
		if [ -e $LOCAL_FILENAME ] && [ "$LOCAL_FILENAME" != "$FILE" ]; then
            msg "$(gettext "Removing %s")" "$LOCAL_FILENAME"
			rm -f $LOCAL_FILENAME
		fi
	done
}


interrupted() {
	if [ "$PKGMK_KEEP_WORK" == "no" ]; then
		rm -rf $PKGMK_WORK_DIR
	fi
    printf "${RED_MSG}\n==> $(gettext "ERROR:")${ALL_OFF_MSG}${BOLD_MSG} Interrupted${ALL_OFF_MSG}\n" >&2
	exit 1
}

print_help() {
	echo "usage: `basename $PKGMK_COMMAND` [options]"
	echo "options:"
	echo "  -kw,  --keep-work           keep temporary working directory"
	echo "  -cf,  --config-file <file>  use alternative configuration file"
	echo "  -v,   --version             print version and exit "
	echo "  -h,   --help                print help and exit"
}
parse_options() {
	while [ "$1" ]; do
		case $1 in
			-kw|--keep-work)
				PKGMK_KEEP_WORK="yes" ;;
			-cf|--config-file)
				if [ ! "$2" ]; then
					abort "$(gettext "%s: option %s requires an argument")" "$(basename $PKGMK_COMMAND)" "$1"
				fi
				PKGMK_CONFFILE="$2"
				shift ;;
			-v|--version)
                msg "$(gettext  "%s %s")" "$(basename $PKGMK_COMMAND)" "$PKGMK_VERSION"
				exit 0 ;;
			-h|--help)
				print_help
				exit 0 ;;
			*)
                abort "$(gettext "%s:  invalid option %s")" "$(basename $PKGMK_COMMAND)" "$1"
                ;;
		esac
		shift
	done
}

main() {
	local FILE TARGET

	parse_options "$@"

	for FILE in $PKGMK_CONFFILE $PKGMK_PKGFILE ; do
		if [ ! -f $FILE ]; then
			abort "$(gettext "File '%s' not found.")" "$FILE"
		fi
		. $FILE
	done

	check_directory "$PKGMK_SOURCE_DIR"
	check_directory "$PKGMK_PACKAGE_DIR"
	check_directory "`dirname $PKGMK_WORK_DIR`"

	check_pkgfile

	TARGET="$PKGMK_PACKAGE_DIR/$PKGMK_PKGFILE"

	if [ "$PKGMK_CLEAN" = "yes" ]; then
		clean
		exit 0
	fi
		build_package
	exit 0
}

export LC_ALL=POSIX

readonly PKGMK_VERSION="0.0.1"
readonly PKGMK_COMMAND="$0"
readonly PKGMK_ROOT="$PWD"

PKGMK_CONFFILE="$HOME/config/pkgmk.conf"
PKGMK_PKGFILE="Pkgfile"
PKGMK_SOURCE_DIR="/sources"
PKGMK_PACKAGE_DIR="$PWD"
PKGMK_WORK_DIR="/tmp/work"

PKGMK_FORCE="no"
PKGMK_KEEP_WORK="no"
PKGMK_NO_STRIP="no"
PKGMK_CLEAN="no"
main "$@"

# End of file

